(function ($app) {
    $app.module('gtPartyApp').controller('jlcfxxeditCtrl', function ($scope, $http, $modal, $ocLazyLoad, $alert, $loading, $state, $partySelect, $stateParams, $bsRouterState, $partySelect) {
        $stateParams = $bsRouterState.$params($scope);

        $scope.maxDate = moment(new Date()).format("YYYY-MM-DD");

        $scope.isDetail =($stateParams.isDetail == "true"||$stateParams.isDetail==true);
        //定义数据集合
        $scope.dataModel = {
            id: $stateParams.id == undefined ? "" : $stateParams.id,
            dzzdm: $stateParams.dzzdm,
            dzzmc: $stateParams.dzzmc
        };
        $scope.judgeLevelList=[];

        $scope.reqFileModel = {
            selectdata: {FileRefID: '', pageindex: 1, pagesize: 10, ptotal: 0},//查询参数：FileRefID为文件关联ID
            filetype: 3,//文件类型
            items: [],//列表数据地址
            readonly:    !$scope.isDetail,//是否只能查看
            title: '附件（处分文件等）',
            addFileButton: '上传'
        };
        $scope.selectUserlist = {
            url: '../../api/partyUser/getPartyUserList',
            title: '人员选择',
            isMulti: false,
            selectData: {xm: '姓名', szdzb: '所在组织架构'},
            columns: {XM: '姓名', XBMC: '性别', JG: '籍贯', DZZMC: '所在组织架构'},
            selectLabelKey: 'XM',
            selectValuekey: 'RYBM',
            selectedValueData: '',
            selectedLabeData: '',
            selectedJData: [],
            selectParams: {
                dzzdm: $stateParams.dzzdm
            }
        };
        $scope.$watch('selectUserlist.selectedValueData', function (newVal) {
            if (newVal != '' && newVal != undefined) {
                $http({
                    method: 'get',
                    url: '../../api/partyUser/getPartyUserList',
                    params: {
                        rybm: newVal,
                        pageindex: 1,
                        pagesize: 1000
                    }
                }).then(function (result) {
                    if (result.data.item.list.length > 0) {
                        var user = result.data.item.list[0];
                        $scope.dataModel.userid = user.RYBM;
                        $scope.dataModel.username = user.XM;
                    }
                }, function (resp) {
                    $scope.showMsg('错误', '服务器错误');
                });
            }
        });

        //选择党支部
        $scope.partyTreeConfig = {
            scope: $scope,
            selectedKey: '',
            selectedText: '',
            selectedData: [],
            isMulti: false,
            isshowxz: 0
        };
        $partySelect.initServer($scope.partyTreeConfig);
        $scope.selectParty = function () {
            $partySelect.showModal();
        };
        $scope.$watch("partyTreeConfig.selectedKey", function (newVal, oldVal) {
            if (newVal) {
                $scope.dataModel.dzzdm = $scope.partyTreeConfig.selectedKey;
                $scope.dataModel.dzzmc = $scope.partyTreeConfig.selectedText;
                $scope.selectUserlist.selectParams.dzzdm= $scope.partyTreeConfig.selectedKey;
            }
        }, true);

        $scope.loadData = function () {
            $http.get("../../api/partyTowExt/getJlcfxx", {
                params: {
                    'id': $scope.dataModel.id,
                    'dzzdm': $scope.dataModel.dzzdm,
                    'dzzmc': $stateParams.dzzmc
                }
            }).then(function (result) {
                $scope.dataModel = result.data.item;
                $scope.reqFileModel.selectdata.FileRefID=$scope.dataModel.id;
            }, function () {
                $scope.showMsg('错误', '服务器错误');
            });
        };
        //保存
        $scope.submitData = function (isflag) {
            if (isflag) {
                $loading.show();
                $http({
                    method: "post",
                    url: "../../api/partyTowExt/saveJlcfxx",
                    data: $scope.dataModel
                }).then(function (result) {
                    $loading.hide();
                    $scope.showMsg('提示', result.data.msg);

                    $scope.$emit("tabReloadData",{name:'jlcfxxCtrl',data:1});
                    //重新刷新列表
                    $scope.pagechange();
                    $scope.$hide();

                }, function (resp) {
                    $scope.showMsg('错误', '服务器错误');
                })
            }
        };

        $scope.getJudgeLevelList = function () {
            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {
                    dicTypeKey: 'punishType'
                }
            }).then(function (result) {
                $scope.judgeLevelList = result.data;
            }, function (resp) {

            });
        };
        $scope.getJudgeLevelName = function () {
            var items = $scope.judgeLevelList.filter(function (item) {
                return item.dickey == $scope.dataModel.punishtype;
            })
            return items.length > 0 ? items[0].dicvalue : "";
        }
        $scope.getoffencetypeList = function () {
            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {
                    dicTypeKey: 'offenceType'
                }
            }).then(function (result) {
                $scope.offenceTypeList = result.data;
            }, function (resp) {

            });
        };
        $scope.getoffencetypeName = function () {
            var items = $scope.offenceTypeList.filter(function (item) {
                return item.dickey == $scope.dataModel.offencetype;
            })
            return items.length > 0 ? items[0].dicvalue : "";
        }
        //加载数据
        $scope.loadData();
        $scope.getJudgeLevelList();
        $scope.getoffencetypeList();

        $scope.pagechange = function () {
            var inTab = $bsRouterState.$getRouteType() == 'tab';
            if (inTab) {
                $bsRouterState.$closeTab($scope);
            } else {
                window.history.back();
            }
        };

        $scope.showMsg = function (title, content) {
            $alert({
                title: title + '：',
                content: content,
                placement: 'top',
                type: 'info',
                show: true,
                duration: 3
            });

        };

    });
})(angular);